package com.htfyun.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.htfyun.utils.DebugLog;

import org.geometerplus.zlibrary.ui.android.R;


/**
 * Created by YoKeyword on 16/6/3.
 */
public class BottomBarTab extends FrameLayout {
    private ImageView mIcon;
    private TextView mTvTitle;
    private Context mContext;
    private int mTabPosition = -1;

    private float imgSizeNormal;
    private float imgSizeSelected;


    public BottomBarTab(Context context, @DrawableRes int icon, CharSequence title) {
        this(context, null, icon, title);
    }

    public BottomBarTab(Context context, AttributeSet attrs, int icon, CharSequence title) {
        this(context, attrs, 0, icon, title);
    }

    public BottomBarTab(Context context, AttributeSet attrs, int defStyleAttr, int icon, CharSequence title) {
        super(context, attrs, defStyleAttr);
        init(context, icon, title);
    }

    private void init(Context context, int icon, CharSequence title) {
        mContext = context;
        TypedArray typedArray = context.obtainStyledAttributes(new int[]{R.attr.selectableItemBackground, R.attr.titleTextSize,
                R.attr.barTabImgSizeNormal, R.attr.barTabImgSizeSelected});

        Drawable drawable = typedArray.getDrawable(0);

        float textSize = typedArray.getDimension(1, dip2px(mContext, 20));

        imgSizeNormal = typedArray.getDimension(2, dip2px(mContext, 27));
        imgSizeSelected = typedArray.getDimension(3, dip2px(mContext, 37));
        
        typedArray.recycle();

        RelativeLayout rLContainer = new RelativeLayout(context);
        rLContainer.setGravity(Gravity.CENTER);

        rLContainer.setBackground(drawable);
//        rLContainer.setPadding(dip2px(mContext, 50), 0, dip2px(mContext, 50), 0);

        LayoutParams paramsContainer = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        paramsContainer.gravity = Gravity.CENTER;
        paramsContainer.setMarginEnd(dip2px(mContext, 50));
        paramsContainer.setMarginStart(dip2px(mContext, 50));
        rLContainer.setLayoutParams(paramsContainer);

        if (icon > 0) {
            mIcon = new ImageView(context);
            int size = (int) imgSizeNormal;//TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 27, getResources().getDisplayMetrics());
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(size, size);
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            mIcon.setImageResource(icon);
            mIcon.setLayoutParams(params);
            
            rLContainer.addView(mIcon); 
        }

        mTvTitle = null;
        if (!TextUtils.isEmpty(title)) {
            mTvTitle = new TextView(context);
            mTvTitle.setText(title);
//            LinearLayout.LayoutParams paramsTv = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics());

            DebugLog.e("size  = " + size);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(size, size);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params.addRule(RelativeLayout.CENTER_HORIZONTAL);
            params.topMargin = 20;
                    

            mTvTitle.setTextSize(textSize);
            mTvTitle.setTextColor(Color.BLACK);
            mTvTitle.setLayoutParams(params);
            mTvTitle.setBackgroundResource(R.drawable.ic_arrow_drop_up_black_24dp);

            mTvTitle.setVisibility(INVISIBLE);
            rLContainer.addView(mTvTitle);

        }
//
//        
        addView(rLContainer);



//        LinearLayout lLContainer = new LinearLayout(context);
//        lLContainer.setOrientation(LinearLayout.VERTICAL);
//        lLContainer.setGravity(Gravity.CENTER);
//        LayoutParams paramsContainer = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        paramsContainer.gravity = Gravity.CENTER;
//        lLContainer.setLayoutParams(paramsContainer);
//
//        if (icon > 0) {
//            mIcon = new ImageView(context);
//            int size = (int) imgSizeNormal;//TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 27, getResources().getDisplayMetrics());
//            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(size, size);
//            mIcon.setImageResource(icon);
//            mIcon.setLayoutParams(params);
////            mIcon.setColorFilter(context.getResources().getColor(R.color.tab_unselect));
//            lLContainer.addView(mIcon); 
//        }
//
//        mTvTitle = null;
//        if (true || !TextUtils.isEmpty(title)) {
//            mTvTitle = new TextView(context);
//            mTvTitle.setText(title);
//            LinearLayout.LayoutParams paramsTv = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//            paramsTv.topMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
//            mTvTitle.setTextSize(textSize);
////        mTvTitle.setTextColor(context.getResources().getColor(R.color.tab_unselect));
//            mTvTitle.setTextColor(Color.BLACK);
//            mTvTitle.setLayoutParams(paramsTv);
//            lLContainer.addView(mTvTitle);
//
//        }
//
//        
//        addView(lLContainer);
      
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);

        if (mTvTitle != null) {
            if (selected) {
//                mTvTitle .setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
//                mTvTitle.setText("*");
                mTvTitle.setVisibility(VISIBLE);
            } else {
                mTvTitle.setVisibility(INVISIBLE);

//                mTvTitle.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
//                mTvTitle.setText("");

            }
        }

        if (mIcon != null) {

//            mIcon.getLayoutParams().width = mIcon.getLayoutParams().height = selected ? (int) imgSizeSelected : (int) imgSizeNormal;
//                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)mIcon.getLayoutParams();
//            
//            if (selected) {
//                
//                params.width = (int)imgSizeSelected;
//                params.height = (int)imgSizeSelected;
//            } else {
//                params.width = (int)imgSizeNormal;
//                params.height = (int)imgSizeNormal;
//            }
//            mIcon.setLayoutParams(params);
        }
      
       
        
//        if (selected) {
//            if (mIcon != null) {
//                mIcon.setColorFilter(mContext.getResources().getColor( R.color.colorPrimary));
//            }
//            mTvTitle.setTextColor(mContext.getResources().getColor( R.color.colorPrimary));
//        } else {
//            if (mIcon != null) {
//                mIcon.setColorFilter(mContext.getResources().getColor(R.color.tab_unselect));
//            }
//            
//            mTvTitle.setTextColor(mContext.getResources().getColor(R.color.tab_unselect));
//        }
    }

    public void setTabPosition(int position) {
        mTabPosition = position;
        if (position == 0) {
            setSelected(true);
        }
    }

    public int getTabPosition() {
        return mTabPosition;
    }

   
    private int dip2px(Context context, float dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
    }
}
